﻿using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Configuration;

namespace Freedom.Common
{
    public class Email
    {
        #region variables
        private readonly string _strSmtpServer;
        private readonly string _strFrom;
        private readonly string _strPassword;
        #endregion

        #region contructors
        public Email(string strSmtpServer, string strFrom, string strPwd)
        {
            _strSmtpServer = strSmtpServer;
            _strFrom = strFrom;
            _strPassword = strPwd;
        }
        /// <summary>
        /// need to add the node "SMTP" into config file
        /// </summary>
        /// <param name="strFrom"></param>
        /// <param name="strPwd"></param>
        public Email(string strFrom, string strPwd)
        {
            _strSmtpServer = ConfigurationManager.AppSettings["SMTP"];
            _strFrom = strFrom;
            _strPassword = strPwd;
        }
        /// <summary>
        /// need to add the node "SMTP" into config file
        /// </summary>
        /// <param name="strFrom"></param>
        public Email(string strFrom)
        {
            _strSmtpServer =  ConfigurationManager.AppSettings["SMTP"];
            _strFrom = strFrom;
            _strPassword = "";
        }
        /// <summary>
        /// need to add the nodes "SMTP" and "EmailFrom" into config file
        /// </summary>
        public Email()
        {
            _strSmtpServer = ConfigurationManager.AppSettings["SMTP"];
            _strFrom =  ConfigurationManager.AppSettings["MailFrom"];
            _strPassword = "";
        }
        #endregion

        /// <summary>
        /// method to send a full parameters email
        /// </summary>
        /// <param name="strSubject"></param>
        /// <param name="strBody"></param>
        /// <param name="strTo"></param>
        /// <param name="strSender"></param>
        /// <param name="strCC"></param>
        /// <param name="strBCC"></param>
        /// <param name="strReplyTo"></param>
        /// <param name="blIsBodyHtml"></param>
        /// <param name="ecdBody"></param>
        /// <param name="objAttachments"></param>
        public void Send(string strSubject, string strBody, string strTo,string strSender, string strCC, string strBCC, string strReplyTo, bool blIsBodyHtml, Encoding ecdBody, IList<Attachment>  objAttachments)
        {
            //split receivers
            string[] strReceivers = strTo.Split(new[] { ',', ';' });
            //new a email object
            var objEmail = new MailMessage();
            //create a smtp server
            var objSmtp = new SmtpClient(_strSmtpServer);
            //no password need no credentials
            if (!string.IsNullOrEmpty(_strPassword))
            {
                var objNetwordCred = new NetworkCredential(_strFrom, _strPassword);
                objSmtp.UseDefaultCredentials = false;
                objSmtp.Credentials = objNetwordCred;
            }
            objEmail.From = new MailAddress(_strFrom);
            //add receivers
            foreach (string strReceiver in strReceivers)
            {
                objEmail.To.Add(strReceiver);
            }
            //add subject
            objEmail.Subject = strSubject;
            //add email body
            objEmail.Body = strBody;
            if (!string.IsNullOrEmpty(strSender))
            {
                objEmail.Sender = new MailAddress(strSender);
            }
            if (!string.IsNullOrEmpty(strReplyTo))
            {
                objEmail.ReplyTo = new MailAddress(strReplyTo);
            }
            objEmail.IsBodyHtml = blIsBodyHtml;
            objEmail.BodyEncoding = ecdBody;
            if (objAttachments != null && objAttachments.Count > 0)
            {
                foreach (Attachment objAttachment in objAttachments)
                {
                    objEmail.Attachments.Add(objAttachment);
                }
            }
            objSmtp.Send(objEmail);
        }
        /// <summary>
        /// mothod to send a simple parameters email
        /// </summary>
        /// <param name="strSubject"></param>
        /// <param name="strBody"></param>
        /// <param name="strTo"></param>
        public void Send(string strSubject, string strBody, string strTo)
        {
            Send(strSubject, strBody, strTo, "", "", "", "", false, Encoding.UTF8, null);
        }
        /// <summary>
        /// need to add the node "EmailTo" into config file
        /// </summary>
        /// <param name="strSuject">subject</param>
        /// <param name="strBody">body</param>
        public void Send(string strSuject, string strBody)
        {
            string strTo = ConfigurationManager.AppSettings["MailTo"];
            Send(strSuject, strBody, strTo, "", "", "", "", false, Encoding.UTF8, null);
        }
    }
}
